﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using myBoard.App.Model;
using myBoard.App.MagnetStore;
using myBoard.App.Components.MagnetsStore;
using System.Collections.ObjectModel;
using myBoard.App.Components.MagnetsStore.ViewsModels;
using myBoard.Framework;
using System.Windows.Media.Animation;

namespace myBoard.App
{
	/// <summary>
    /// Interaction logic for MainScreen.xaml
	/// </summary>
	public partial class MainScreen : UserControl
	{
        private Storyboard _swichToMagnetScreenSB;
        private Storyboard _switchToPreviousScreenSB;
        private bool _isInMagnetDetailsMode;

		public MainScreen()
		{
			this.InitializeComponent();
            _submittedMagnetsLbx.SelectionChanged += new SelectionChangedEventHandler(_submittedMagnetsLbx_SelectionChanged);
            _usedCategoriesLbx.SelectionChanged += new SelectionChangedEventHandler(_usedCategoriesLbx_SelectionChanged);
            _swichToMagnetScreenSB = this.Resources["SwitchToMagnetScreen"] as Storyboard;
            _switchToPreviousScreenSB = this.Resources["SwitchToPreviousScreen"] as Storyboard;
            _magnetScreen.CloseRequested += new EventHandler(_magnetScreen_CloseRequested);
            _searchBt.Click += new RoutedEventHandler(SearchBt_Click);
            _searchTbx.GotFocus += new RoutedEventHandler(SearchTbx_GotFocus);
            _searchTbx.KeyUp += new KeyEventHandler(SearchTbx_KeyUp);
            InstancesSingleton.Instances.MagnetsViewModel.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(MagnetsViewModel_PropertyChanged);
            InstancesSingleton.Instances.MagnetsViewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(MagnetsViewModel_PropertyChanged);

		}

        void SearchTbx_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                SearchBt_Click(sender, e);
        }

        void SearchTbx_GotFocus(object sender, RoutedEventArgs e)
        {
            _searchTbx.Text = string.Empty;
        }

        void SearchBt_Click(object sender, RoutedEventArgs e)
        {
            Instances instances = new Instances();
            MagnetsStoreClient.Client.SearchMagnetCompleted -= new EventHandler<SearchMagnetCompletedEventArgs>(Client_SearchMagnetCompleted); 
            MagnetsStoreClient.Client.SearchMagnetCompleted += new EventHandler<SearchMagnetCompletedEventArgs>(Client_SearchMagnetCompleted);
            MagnetsStoreClient.Client.SearchMagnetAsync(instances.UsersViewModel.ConnectedUserSessionId, _searchTbx.Text);
        }

        void Client_SearchMagnetCompleted(object sender, SearchMagnetCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                ExceptionsManager.LogException("Magnets Store","Search failed", e.Error, System.Diagnostics.EventLogEntryType.Error, true);
            }
            if (e.Result.Count == 0)
            {
                myBoard.Framework.Forms.MessageBox.Show("No magnet found", "No magnet matching your search has been found.", "Magnet Store");
            }
            ObservableCollection<Magnet> magnets = e.Result;
            Instances instances = new Instances();
            instances.MagnetsViewModel.ActiveCategoryMagnets.Clear();
            foreach (Magnet magnet in magnets)
            {
                instances.MagnetsViewModel.ActiveCategoryMagnets.Add(new MagnetViewModel(magnet));
            }
            instances.MagnetsViewModel.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(MagnetsViewModel_PropertyChanged);
            instances.MagnetsViewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(MagnetsViewModel_PropertyChanged);
        }

        void _magnetScreen_CloseRequested(object sender, EventArgs e)
        {
            this.BeginStoryboard(_switchToPreviousScreenSB);
            _isInMagnetDetailsMode = false;
        }

        void _usedCategoriesLbx_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (_usedCategoriesLbx.SelectedItem != null)
            {
                CategoryViewModel cvm = _usedCategoriesLbx.SelectedItem as CategoryViewModel;
                MagnetsStoreClient.Client.GetMagnetsInCategoryCompleted -= new EventHandler<GetMagnetsInCategoryCompletedEventArgs>(Client_GetMagnetsInCategoryCompleted);
                MagnetsStoreClient.Client.GetMagnetsInCategoryCompleted += new EventHandler<GetMagnetsInCategoryCompletedEventArgs>(Client_GetMagnetsInCategoryCompleted);
                MagnetsStoreClient.Client.GetMagnetsInCategoryAsync(InstancesSingleton.Instances.UsersViewModel.ConnectedUserSessionId, cvm.Id,0,20);
            }
        }

        void Client_GetMagnetsInCategoryCompleted(object sender, GetMagnetsInCategoryCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                ExceptionsManager.LogException("Magnets Store", e.Error, true);
            }
            ObservableCollection<Magnet> magnets = e.Result;
            Instances instances = new Instances();
            instances.MagnetsViewModel.ActiveCategoryMagnets.Clear();
            foreach(Magnet magnet in magnets)
            {
                instances.MagnetsViewModel.ActiveCategoryMagnets.Add(new MagnetViewModel(magnet));                
            }
        }

        void MagnetsViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "SelectedMagnet")
            {
                Instances instances = new Instances();
                if (instances.MagnetsViewModel.SelectedMagnet != null)
                {
                    if (_magnetScreen.DataContext != null && !_isInMagnetDetailsMode)
                    {
                        this.BeginStoryboard(_swichToMagnetScreenSB);
                        _isInMagnetDetailsMode = true;
                    }
                }
            }
        }

        void _submittedMagnetsLbx_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (_submittedMagnetsLbx.SelectedItem != null)
            {
                Instances instances = new Instances();
                instances.MagnetsViewModel.CurrentlyEditingMagnet = _submittedMagnetsLbx.SelectedItem as Magnet;
                _magnetSubmissionForm.Visibility = Visibility.Visible;
            }
        }
	}
}